//
//  NewFeatureController.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/9.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
import SnapKit
private let newPages = 4
private let reuseIdentifier = "NewCell"

class NewFeatureController: UICollectionViewController {

    //重写构造方法,布局子控件参数
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()


        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }

    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return newPages
    }

    //给imageIndex赋值设置图片
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
    
    //停止滚动开始动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        let lastPage = newPages - 1
        
        if page == lastPage{
           let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forRow: page, inSection: 0)) as! NewFeatureCell
            
            cell.startAnimation()
        }
    }
}


//自定义控制器
private class NewFeatureCell :UICollectionViewCell{
    //给cell设置图片
    var imageIndex: Int = 0{
        didSet{
           iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
           starButton.hidden = true
        }
    }
    
    //增加监听方法
    @objc private func clickStartButton(){
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil, userInfo: nil)
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //开始动画
    private func startAnimation(){
        starButton.hidden = false
        starButton.transform = CGAffineTransformMakeScale(0, 0)
        starButton.userInteractionEnabled = false
        
       UIView.animateWithDuration(1.6, //动画时长
        delay: 0,                      //动画延时
        usingSpringWithDamping: 0.8,   //回填系数 0 ~ 1越小反弹越大，一般0.8
        initialSpringVelocity: 10,     //模拟重力加速度，10
        options: [],                   //动画选项
        animations: { () -> Void in
            self.starButton.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            self.starButton.userInteractionEnabled = true
        }
    }
    
    //设置控件信息
    private func setUI(){
        addSubview(iconView)
        addSubview(starButton)
        iconView.frame = self.bounds
        starButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
        starButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
    }
    
    //定义两个控件
    private lazy var iconView: UIImageView = UIImageView()
    private lazy var starButton: UIButton = UIButton(backimageName: "new_feature_finish_button", title: "开始体验", color: UIColor.whiteColor())
}
